import StarterKit from "@tiptap/starter-kit";
import { TextStyle } from "@tiptap/extension-text-style";
import Color from "@tiptap/extension-color";
import CustomTextAlign from "./textAlign";
import Underline from "@tiptap/extension-underline";
import Link from "@tiptap/extension-link";
import Image from "@tiptap/extension-image";
import { Table } from "@tiptap/extension-table";
import TableRow from "@tiptap/extension-table-row";
import TableHeader from "./table/header";
import TableCell from "./table/cell";
import TaskList from "@tiptap/extension-task-list";
import TaskItem from "@tiptap/extension-task-item";
import Placeholder from "@tiptap/extension-placeholder";
import CharacterCount from "@tiptap/extension-character-count";
import Gapcursor from "@tiptap/extension-gapcursor";
import Dropcursor from "@tiptap/extension-dropcursor";
import HardBreak from "@tiptap/extension-hard-break";
import HorizontalRule from "@tiptap/extension-horizontal-rule";
import Blockquote from "@tiptap/extension-blockquote";
import Code from "@tiptap/extension-code";
import Superscript from "@tiptap/extension-superscript";
import Subscript from "@tiptap/extension-subscript";
import Highlight from "@tiptap/extension-highlight";
import FontFamily from "@tiptap/extension-font-family";
import Typography from "@tiptap/extension-typography";
import CodeBlockLowlight from "@tiptap/extension-code-block-lowlight";
import Focus from "@tiptap/extension-focus";
import { createLowlight } from "lowlight";
import FormatBrush from "./formatBrush";
import CustomOrderedList from "./orderedList";
import CustomBulletList from "./bulletList";
import FontSize from "./fontSize";
import Indent from "./indent";
import LineHeight from "./lineHeight";
import { PageBreak } from "./pageBreak";
import { Comment } from "./comment";
import { Toc } from "./toc";

/**
 * 获取默认的Tiptap扩展配置
 * @param editorProps 编辑器属性配置
 * @returns 默认扩展数组
 */
export function getDefaultExtensions(editorProps?: {
  defaultLineHeight?: number | string;
}) {
  return [
    StarterKit.configure({
      // 禁用StarterKit中与单独扩展重复的功能
      link: false,
      underline: false,
      gapcursor: false,
      dropcursor: false,
      hardBreak: false,
      horizontalRule: false,
      blockquote: false,
      code: false,
      codeBlock: false,
      orderedList: false, // 禁用默认的有序列表，使用自定义版本
      bulletList: false, // 禁用默认的无序列表，使用自定义版本
    }),
    TextStyle,
    Color,
    CustomTextAlign.configure({
      types: ["heading", "paragraph"],
    }),
    Underline,
    Link.configure({
      openOnClick: false,
      HTMLAttributes: {
        class: "text-blue-500 underline",
      },
    }),
    Image.configure({
      // 启用原生的 resize 功能
      resize: {
        enabled: true,
        directions: ["top-left", "top-right", "bottom-left", "bottom-right"], // 只使用四个角点，避免重叠
        minWidth: 50,
        minHeight: 50,
        alwaysPreserveAspectRatio: true, // 始终保持宽高比
      },
      // 允许 base64 图片
      allowBase64: true,
      // 非内联
      inline: false,
    }),
    Table.configure({
      resizable: true,
    }),
    TableRow,
    TableHeader,
    TableCell,
    TaskList,
    TaskItem.configure({
      nested: true,
    }),
    Placeholder.configure({
      placeholder: "开始编写你的内容...",
    }),
    CharacterCount,
    Gapcursor,
    Dropcursor.configure({
      color: "var(--editor-primary-color)",
      width: 2,
    }),
    HardBreak,
    HorizontalRule,
    Blockquote,
    Code,
    Superscript,
    Subscript,
    Highlight.configure({
      multicolor: true,
    }),
    FontFamily.configure({
      types: ["textStyle"],
    }),
    Typography,
    CodeBlockLowlight.configure({
      lowlight: createLowlight(),
    }),
    Focus.configure({
      className: "editor-node-focused",
      mode: "all",
    }),
    FormatBrush,
    CustomOrderedList,
    CustomBulletList,
    FontSize,
    Indent,
    LineHeight.configure({
      types: ["paragraph", "heading"],
      defaultLineHeight: editorProps?.defaultLineHeight ?? 1.5,
    }),
    PageBreak,
    Comment,
    Toc,
  ];
}
